import shutil,os,subprocess
import requests
from validator.Validator import Validator
import random
import uuid


def set_squid_conf():
    '''
    backup squid.conf and cp this conf to /etc/squid
    '''
    squid_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),'squid.conf')
    if os.path.exists('/etc/squid/squid.conf_bak'):
        if not os.path.exists('/etc/squid/squid.conf'):
            print('please check squid')
        else:
            shutil.copyfile(squid_path,'/etc/squid/squid.conf')
    elif os.path.exists('/etc/squid/squid.conf'):
        shutil.copyfile('/etc/squid/squid.conf','/etc/squid/squid.conf_bak')
        shutil.copyfile(squid_path,'/etc/squid/squid.conf')

def set_peers_conf(proxies,auth_proxies):
    '''
    cp peers conf to /etc/squid
    '''
    open_templete = 'cache_peer {IP} parent {PORT} 0 round-robin proxy-only no-query connect-fail-limit=2 name={name}\n '
    secret_templete = 'cache_peer {IP} parent {PORT} 0 login={username}:{password} round-robin proxy-only no-query connect-fail-limit=2 name={name}\n'

    with open('/etc/squid/peers.conf','wt')as f:
    # with open('peers.conf','wt')as f:
        for proxy in auth_proxies:
            f.write(secret_templete.format(IP=proxy[0],PORT=proxy[1],username=proxy[2],password=proxy[3],name=str(uuid.uuid1()).replace('-','')))
        for proxy in proxies:
            try:
                f.write(open_templete.format(IP=proxy[0],PORT=proxy[1],name=str(uuid.uuid1()).replace('-','')))
            except Exception as e:
                print(proxy,e)

def get_proxies():
    set_id = set()
    def duplicate_list(lis):
        if lis[0] in set_id:
            return False
        else:
            set_id.add(lis[0])
            return True

    ret_list = []
    auth_list = []

    # static
    static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),'static_proxy.csv')
    if os.path.exists(static_path):
        with open('static_proxy.csv')as f:
            ret_list = list(map(lambda x:x.strip().split(':'),f.readlines()))

    static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),'auth_proxy.csv')
    if os.path.exists(static_path):
        with open('auth_proxy.csv')as f:
            auth_list = list(map(lambda x:x.strip().split(':'),f.readlines()))
        
    # IPPROXY
    req = requests.get('http://192.168.1.220:9000/get')
    ret_list.extend(req.json())
    req = requests.get('http://192.168.1.220:9001/')
    ret_list.extend(req.json())

    # qiniu
    r = requests.get('http://7xrnwq.com1.z0.glb.clouddn.com/proxy_list.txt?v=%s'%random.randint(1,900))
    for line in r.text.split('\n'):
        ret_list.append(line.split(':'))

    # duplicate list
    ret_list = list(filter(duplicate_list,ret_list))
    print('have %s proxy'%len(ret_list))

    ret_list = list(filter(lambda x:x,ret_list))
    auth_list = list(filter(lambda x:x,auth_list))
    return ret_list,auth_list

def config():
    vd = Validator()
    # set_squid_conf()

    proxies,auth_proxies = get_proxies()
    # print(proxies)
    ps = []
    for each in proxies:
        try:
            ps.append('%s:%s'%(each[0],each[1]))
        except:
            pass

    ret = vd.run_in_multiprocess(ps)
    print(len(ret))
    # print(ret)
    ret = list(map(lambda x:[x.split(':')[0],x.split(':')[1]],ret))
    set_peers_conf(ret,auth_proxies)
    subprocess.run(["service","squid","reload"],check=True)

if __name__ == '__main__':
    import time
    for _ in range(99999):
        config()
        time.sleep(5*60)

    # r = requests.get('http://7xrnwq.com1.z0.glb.clouddn.com/proxy_list.txt?v=%s'%random.randint(1,900))
    # for line in r.text.split('\n'):
    #     print(line)
    # config()
